import http from "../httpService"
import { UserShippingAddressInputDto } from "../cart/dto/userShippingAddressInputDto"
import { AreaForCascaderModel } from "../cart/dto/areaForCascaderModel"
import { ProductForOrder } from "./dto/productForOrder"
import { OrderModel } from "./dto/orderModel"
import { MyOrderPagedQueryDto } from "./dto/myOrderPagedQueryDto"
import { ProductForShopQueryDto } from "./dto/productForShopQueryDto"
import { OrderScoreInputDto } from "./dto/orderScoreInputDto"
import { OrderCountDto } from "./dto/orderCountDto";
class OrderService {
    public async getMyAddressList() {
        return await http.get('/api/services/app/Order/GetMyUserShippingAddressList', {})
    }

    public async createOrUpdateUserShippingAddressAsync(input: UserShippingAddressInputDto) {
        return await http.post('/api/services/app/Order/CreateOrUpdateUserShippingAddressAsync', input)
    }

    public async getUserShippingAddress(id: number) {
        return await http.get('/api/services/app/Order/GetUserShippingAddressAsync', { id: id })
    }

    public async getMyDefaultShippingAddress() {
        return await http.get('/api/services/app/Order/GetMyDefaultUserShippingAddressAsync', {})
    }

    public async getAreas(id: string) {
        return await http.get('/api/Area/GetAreaChildren', { parentId: id })
    }

    public async deleteUserShippingAddress(id: number) {
        let url_ = '/api/services/app/Order/DeleteUserShippingAddressAsync?'
        if (id !== undefined)
            url_ += "Id=" + encodeURIComponent("" + id) + "&"
        return await http.delete(url_, {})
    }

    public async getAreaAndParents(id: string): Promise<AreaForCascaderModel[]> {
        let result = await http.get('/api/Area/GetAreaAndParents', { id: id })
        return result.data.result
    }

    public async getAreaByName(name: string): Promise<AreaForCascaderModel> {
        let result = await http.get('/api/Area/GetAreaByName', { name: name })
        return result.data.result
    }

    public async getAreaParentsAndSiblings(code: string) {
        let result = await http.get('/api/Area/GetAreaParentsAndSiblings?id=' + code, {})
        return result.data.result
    }

    public async getOrderProductsByCartIds(ids: number[]): Promise<ProductForOrder[]> {
        let url_ = '/api/services/app/Order/GetOrderProductsByCartIds?'
        if (ids !== undefined)
            ids && ids.forEach(item => { url_ += 'id=' + encodeURIComponent('' + item) + '&' })
        url_ = url_.replace(/[?&]$/, '')
        let result = await http.get(url_, {})
        return result.data.result
    }

    public async getProductsForShop(input: ProductForShopQueryDto[]): Promise<ProductForOrder[]> {
        let url_ = '/api/services/app/Product/GetProductsForShopAsync?';
        if (input !== undefined)
            input && input.forEach((item, index) => {
                url_ += 'input[' + index + '].quantity=' + item.quantity + '&input[' + index + '].productId=' + item.productId + '&input[' + index + '].skuId=' + item.skuId + '&'
            })
        url_ = url_.replace(/[?&]$/, '')
        let result = await http.get(url_, {})
        return result.data.result
    }


    public async createOrder(model: OrderModel) {
        let result = await http.post('/api/OrderApi/CreateOrder', model)
        return result.data.result
    }

    public async getJsApi() {
        let result = await http.get('/api/WeixinJSSDK/GetJsSdkUiPackage', { url: '' })
        return result.data.result
    }

    public async getApiPay(orderId: string) {
        let contentType = 'application/json-patch+json'
        let result = await http.post('/api/TenpayV3/JsApiPay', { orderId: orderId }, contentType)
        return result.data.result
    }

    public async getPayStatus(orderId: string) {
        let result = await http.get('/api/TenpayV3/GetPayStatus', { orderId: orderId })
        return result.data.result
    }

    public async getMyOrderDetail(orderId: string) {
        let result = await http.get('/api/services/app/Order/GetMyOrderDetailAsync', { id: orderId })
        return result.data.result
    }

    public async getOrderDetail(orderId: string) {
        let result = await http.get('/api/services/app/Order/GetOrderDetailAsync', { id: orderId })
        return result.data.result
    }

    public async getMyOrderPagedList(input: MyOrderPagedQueryDto) {
        let result = await http.get('/api/services/app/Order/GetMyOrderPagedListAsync', input)
        return result.data.result
    }

    public async cancelOrder(id: string) {
        let result = await http.post('/api/services/app/Order/CancleAsync', { id: id })
        return result.data.result
    }

    public async confirmReceive(id: string) {
        let result = await http.post('/api/services/app/Order/ConfirmReceivingAsync', { id: id })
        return result.data.result
    }

    public async deleteOrder(id: string) {
        let url_ = '/api/services/app/Order/DeleteOrderAsync?'
        if (id !== undefined)
            url_ += "Id=" + encodeURIComponent("" + id) + "&"
        await http.delete(url_, {})
    }

    public async createOrderScore(model: OrderScoreInputDto): Promise<number> {
        let url_ = '/api/services/app/Order/CreateOrderScoreAsync'
        let result = await http.post(url_, model)
        return result.data.result
    }

    public async getMyOrderCount(moduleTypeIds: string[]): Promise<OrderCountDto> {
        let url_ = '/api/services/app/Order/GetMyOrderCount?'
        if (moduleTypeIds !== undefined)
            moduleTypeIds && moduleTypeIds.forEach(item => { url_ += 'moduleTypeIds=' + encodeURIComponent('' + item) + '&' })
        url_ = url_.replace(/[?&]$/, '')
        let result = await http.get(url_, {})
        return result.data.result
    }

    public async confirmOrder(orderId: string) {
        let url_ = '/api/services/app/Order/ConfirmAsync'
        let result = await http.post(url_, { id: orderId })
        return result.data.result
    }

    public async changeOrderUserShippingAddress(userShippingAddressId: number, id: string) {
        let url_ = '/api/services/app/Order/ChangeOrderUserShippingAddressAsync'
        let result = await http.post(url_, { userShippingAddressId: userShippingAddressId, id: id })
        return result.data.result
    }
}

export default new OrderService()